package top.wetech.tools.util;

import org.apache.log4j.Logger;
import top.wetech.tools.datetime.DateTimeUtils;
import top.wetech.tools.encrypt.Base64;
import top.wetech.tools.encrypt.MD5Utils;
import top.wetech.tools.encrypt.ShaUtils;
import top.wetech.tools.personal.IdCardUtils;
import top.wetech.tools.personal.NameUtils;
import top.wetech.tools.personal.PhoneUtils;
import top.wetech.tools.string.NumberUtils;
import top.wetech.tools.string.StringUtils;

/**
 * company:
 * user: chenzuoli
 * date: 2018/6/15
 * time: 9:53
 * description: 对列值进行不同方式的清洗，包括但不限于日期、数值、字符、字符串、姓名、身份证号、手机号等
 */
public class ColumnValueEtl {
    private static Logger logger = Logger.getLogger(ColumnValueEtl.class);

    /**
     * description: 按照指定的清洗方法对value进行清洗
     * param: [value, etlMethod]
     * return: java.lang.String
     * date: 2018/6/15
     * time: 13:26
     */
    public static String etl(String value, String etlMethod) {
        if (value == null) return null;
        String returnValue = value.trim();
        try {
            if ("default".equals(etlMethod)) {
                return StringUtils.etlJsonStr(value);
            } else if ("name".equals(etlMethod)) {
                returnValue = NameUtils.etlName(value);
            } else if ("phone".equals(etlMethod)) {
                returnValue = PhoneUtils.etlPhone(value);
            } else if ("idCard".equals(etlMethod)) {
                returnValue = IdCardUtils.etlIdCard(value);
            } else if ("dateTime".equals(etlMethod)) {
                returnValue = DateTimeUtils.dateStr2DateStr(value);
            } else if ("int".equals(etlMethod)) {
                returnValue = NumberUtils.transformFloatStrToIntStr(value);
            } else if ("float".equals(etlMethod)) {
                returnValue = NumberUtils.transformNumberToDouble(value);
            } else if ("money".equals(etlMethod)) {
                returnValue = NumberUtils.moneyEtl(value);
            } else if ("md5_16".equals(etlMethod)) {
                returnValue = MD5Utils.strToMd5_16(value);
            } else if ("md5_32".equals(etlMethod)) {
                returnValue = MD5Utils.strToMd5_32(value);
            } else if ("sha256".equals(etlMethod)) {
                returnValue = ShaUtils.SHA256(value);
            } else if ("sha512".equals(etlMethod)) {
                returnValue = ShaUtils.SHA512(value);
            } else if ("base64_encrypt".equals(etlMethod)) {
                returnValue = Base64.encodeBase64(value.getBytes());
            } else if ("base64_decrypt".equals(etlMethod)) {
                returnValue = new String(Base64.decryptBase64(value));
            } else {
                logger.error("there is no this etl method: " + etlMethod + " to etl value: " + value);
            }
        } catch (Exception e) {
            logger.error("etl value " + value + " with etl method " + etlMethod + " exception!", e);
        }
        return returnValue;
    }

}
